#include"process.h"
#include<unistd.h>
#include<time.h>
#include<stdlib.h>

//函数指针类型
typedef void(*call_t)(const char*,double,double);

double total=1024.0;
double speed[]={20.0,0.5,0.3,0.2,0.01,0.001};

void download(int total,call_t cb)
{
    srand(time(NULL));
    double current=0.0;
    while(current<=total)
    {
        cb("下载中",total,current);//进行回调
        if(current>=total) break;
        //模拟下载代码
        int random=rand()%6;
        usleep(50000);
        current+=speed[random];
        if(current>=total) current=total;
    }
}

void upload(int total,call_t cb)
{
    srand(time(NULL));
    double current=0.0;
    while(current<=total)
    {
        cb("上传中",total,current);//进行回调
        if(current>=total) break;
        //模拟下载代码
        int random=rand()%6;
        usleep(50000);
        current+=speed[random];
        if(current>=total) current=total;
    }
}

int main()
{
    download(1024.0,FlushProcess);
    printf("download 1024.0MB done\n"); 
    download(512.0,FlushProcess);
    printf("download 512.0MB done\n"); 
    upload(500.0,FlushProcess);
    printf("upload 500.0MB done\n"); 
    return 0;
}
